package com.ruoyi.petSys.service.impl;

import java.security.Security;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.petSys.entity.vo.InvitationVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.petSys.mapper.InvitationMapper;
import com.ruoyi.petSys.domain.Invitation;
import com.ruoyi.petSys.service.IInvitationService;

/**
 * 帖子Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-21
 */
@Service
public class InvitationServiceImpl implements IInvitationService 
{
    @Autowired
    private InvitationMapper invitationMapper;

    /**
     * 查询帖子
     * 
     * @param id 帖子主键
     * @return 帖子
     */
    @Override
    public InvitationVo selectInvitationById(Long id)
    {
        InvitationVo invitationVo = invitationMapper.selectInvitationById(id);
        if(StringUtils.isNotBlank(invitationVo.getImgUrl().trim())){
            List<String> imgList = Arrays.stream(invitationVo.getImgUrl().split(";")).collect(Collectors.toList());

            invitationVo.setImgUrlList(imgList);
        }else{
            invitationVo.setImgUrlList(new ArrayList<>());
        }
        if("/dev-api".equals(invitationVo.getAvatar())){
            invitationVo.setAvatar("/icon/profile.jpg");
        }
        return invitationVo;
    }

    /**
     * 查询帖子列表
     * 
     * @param invitation 帖子
     * @return 帖子
     */
    @Override
    public List<InvitationVo> selectInvitationList(Invitation invitation)
    {
        List<InvitationVo> invitationVos = invitationMapper.selectInvitationList(invitation);

        invitationVos = invitationVos.stream().peek(invitationVo -> {
            if(StringUtils.isNotBlank(invitationVo.getImgUrl().trim())){
                List<String> imgList = Arrays.stream(invitationVo.getImgUrl().split(";")).collect(Collectors.toList());

                invitationVo.setImgUrlList(imgList);
            }else{
                invitationVo.setImgUrlList(new ArrayList<>());
            }
            if("/dev-api".equals(invitationVo.getAvatar())){
                invitationVo.setAvatar("/icon/profile.jpg");
            }



        }).collect(Collectors.toList());




        return invitationVos;
    }

    /**
     * 新增帖子
     * 
     * @param invitation 帖子
     * @return 结果
     */
    @Override
    public int insertInvitation(Invitation invitation)
    {

        invitation.setCreatTime(DateUtils.getNowDate());
        invitation.setUpvoteNum(0);
        invitation.setBrowseNum(0);
        invitation.setCheckType(0L);
        invitation.setPromUserId(SecurityUtils.getUserId());
        return invitationMapper.insertInvitation(invitation);
    }

    /**
     * 修改帖子
     * 
     * @param invitation 帖子
     * @return 结果
     */
    @Override
    public int updateInvitation(Invitation invitation)
    {
        invitation.setUpdateTime(DateUtils.getNowDate());
        return invitationMapper.updateInvitation(invitation);
    }

    /**
     * 批量删除帖子
     * 
     * @param ids 需要删除的帖子主键
     * @return 结果
     */
    @Override
    public int deleteInvitationByIds(Long[] ids)
    {
        return invitationMapper.deleteInvitationByIds(ids);
    }

    /**
     * 删除帖子信息
     * 
     * @param id 帖子主键
     * @return 结果
     */
    @Override
    public int deleteInvitationById(Long id)
    {
        return invitationMapper.deleteInvitationById(id);
    }
}
